package com.zhouyibike.uu.ui.activity.yihoudong;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.MydetailResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.listener.MyListener;
import com.zhouyibike.uu.ui.adapter.MydetailAdapter;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.ui.view.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Zy on 2017/10/11.
 */
public class MyYiQuanFragment extends Fragment {
    TextView tv;
    private LinearLayout ll_nothing;
    private PullToRefreshLayout pullToRefreshLayout;
    private ListView lv_allmsg;
    private int refreshtype = 0;
    private int page = 1;//页码
    private boolean ismore = true;//判断是否还有更多数据
    private List<MydetailResult.DataBean> alllist = new ArrayList<>();
    private MydetailAdapter mydetailAdapter;
    private String type = "1";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle b = getArguments();
        View view = View.inflate(getActivity(), R.layout.fragment_myyiquan, null);
        //  type=getIntent().getStringExtra("type");
        try {
            type=b.getString("type");
        } catch (Exception e) {
            e.printStackTrace();
        }
        initView(view);
        getMsg();
       // ((BaseActivity) getActivity()).toastShow(b.getString("type"));
        return view;
    }

    private void initView(View view) {
        pullToRefreshLayout = ((PullToRefreshLayout) view.findViewById(R.id.refresh_view));
        pullToRefreshLayout.setOnRefreshListener(new MyListener2());
        ll_nothing = (LinearLayout) view.findViewById(R.id.ll_nothing_show);
        lv_allmsg = (ListView) view.findViewById(R.id.content_view);
    }

    private void getMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", ((BaseActivity) getActivity()).preferences.getString("token", ""));
        params.put("pageIndex", page + "");
        params.put("type", type);
//        if (page == 1 && refreshtype == 0)
//            ((BaseActivity) getActivity()).showProgressDialog();
        ((BaseActivity) getActivity()).addSubscription(((BaseActivity) getActivity()).apiStores.MyYiQuanDetail(params),
                new ApiCallback<MydetailResult>() {

                    @Override
                    public void onSuccess(MydetailResult model) {
                        String string = new Gson().toJson(model);
                        Log.e("onSuccess: ", string);
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            if (refreshtype == 1) {
                                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                                alllist.clear();
                            } else if (refreshtype == 2) {
                                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                            }
                            if (model.getData().size() == 0) {
                                ismore = false;
                            }
                            alllist.addAll(model.getData());
                            if (alllist.size() == 0) {
                                pullToRefreshLayout.setVisibility(View.GONE);
                                ll_nothing.setVisibility(View.VISIBLE);
                            } else {
                                pullToRefreshLayout.setVisibility(View.VISIBLE);
                                ll_nothing.setVisibility(View.GONE);
                            }
                            initData();

                        } else {
                            ((BaseActivity) getActivity()).toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        ((BaseActivity) getActivity()).toastShow(msg);

                    }

                    @Override
                    public void onFinish() {
                        ((BaseActivity) getActivity()).dismissProgressDialog();
                    }
                });
    }

    private void initData() {
        if (page == 1) {
            mydetailAdapter = new MydetailAdapter(((BaseActivity) getActivity()), alllist);
            lv_allmsg.setAdapter(mydetailAdapter);
        } else {
            mydetailAdapter.setList(alllist);
        }
    }

    class MyListener2 extends MyListener {
        @Override
        public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
            super.onRefresh(pullToRefreshLayout);
            refreshtype = 1;
            page = 1;
            ismore = true;
            getMsg();
        }

        @Override
        public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            super.onLoadMore(pullToRefreshLayout);
            refreshtype = 2;
            if (ismore) {
                page++;
                getMsg();
            } else {
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }
    }
}
